package com.micro.edu.service.impl;

import com.micro.edu.dao.MicroTokenDao;
import com.micro.edu.dao.MicroUserDao;
import com.micro.edu.feign.CodeServiceFeignClient;
import com.micro.edu.pojo.MicroToken;
import com.micro.edu.pojo.MicroUser;
import com.micro.edu.service.UserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MicroUserDao microUserDao;
    @Autowired
    private MicroTokenDao microTokenDao;
    @Autowired
    private CodeServiceFeignClient codeServiceFeignClient;

    public boolean register(String email, String password, String code) {
        if (StringUtils.isBlank(password)) {
            return false;
        }
        if (isRegistered(email)) {
            return false;
        }
        if (codeServiceFeignClient.validateCode(email, code) == 1) {
            MicroUser microUser = new MicroUser();
            microUser.setEmail(email);
            microUser.setPassword(password);
            microUser.setCreatetime(LocalDateTime.now());
            microUserDao.save(microUser);
            return true;
        }
        return false;
    }

    @Override
    public boolean isRegistered(String email) {
        MicroUser microUser = new MicroUser();
        microUser.setEmail(email);
        Example<MicroUser> example = Example.of(microUser);
        Optional<MicroUser> optional = microUserDao.findOne(example);
        return optional.isPresent();
    }

    @Override
    public String login(String email, String password) {
        MicroUser microUser = new MicroUser();
        microUser.setEmail(email);
        Example<MicroUser> userExample = Example.of(microUser);
        Optional<MicroUser> optional = microUserDao.findOne(userExample);
        if (optional.isEmpty()) {
            return "";
        }
        microUser = optional.get();
        if (!microUser.getPassword().equals(password)) {
            return "";
        }

        String token = UUID.randomUUID().toString();
        MicroToken microToken = new MicroToken();
        microToken.setEmail(email);
        Example<MicroToken> tokenExample = Example.of(microToken);
        Optional<MicroToken> one = microTokenDao.findOne(tokenExample);
        if (one.isEmpty()) {
            microToken.setEmail(email);
        } else {
            microToken = one.get();
        }
        microToken.setToken(token);
        microTokenDao.save(microToken);
        System.out.println("登录成功,token:" + token);
        return token;
    }

    @Override
    public String info(String token) {
        MicroToken microToken = new MicroToken();
        microToken.setToken(token);
        Optional<MicroToken> one = microTokenDao.findOne(Example.of(microToken));
        if (one.isEmpty()) {
            return "";
        }
        return one.get().getEmail();
    }
}
